#include "ddl.h"
#include "gpio.h"
#include "led.h"
#include "QG_driver.h"
#include "CAT1.h"
#include "uart_api.h"
#include "lpm.h"
#include "rtc.h"



//uint8_t QG_state_flag;

volatile uint8_t QG_state_start = 0;
volatile uint8_t QG_timer_start = 0; //定时器开始标志
volatile uint8_t QG_state_recover = 0;   //报警恢复标志

extern uint8_t wakeup_from_RI;	//RI中断唤醒标志

extern uint8_t CAT1_state_low_power;

uint8_t pre_QG_state = 0;	//上一次驱狗功能状态

extern volatile int8_t timer_shield_observe;	//观察标志位

volatile uint8_t active_during_watch_flag = 0;	//在观察期触发

volatile uint16_t active_int_counter = 0;

QG_INFO QG_info = {0,0};

extern char dataToSend[SEND_MAX_LEN];

///< PortB中断服务函数
void PortB_IRQHandler(void)
{
	if(TRUE == Gpio_GetIrqStatus(QG_STATE_PORT, QG_STATE_PIN))
	{        
		active_int_counter++;
		// if (QG_state_start == 0)
		// {
			// if (timer_shield_observe == 0)
			// {
			// 	QG_state_start = 1;		 //设置状态标志，表示有红外源触发
			// }
			//观察时间内再次触发，让观察时间加1s
               // if (timer_shield_observe > 0)
               // {
               //     timer_shield_observe = 6;     //重置观察时间
               // }
			QG_state_start = 1;		 //设置状态标志，表示有红外源触发
			QG_timer_start = 1;		//开始屏蔽计时
			QG_info.state = 1;		//功能触发
			if (timer_shield_observe > 0)
			{
				active_during_watch_flag = 1;
			}
			
			Gpio_DisableIrq(QG_STATE_PORT, QG_STATE_PIN, GpioIrqRising);
			// Gpio_DisableIrq(QG_STATE_PORT, QG_STATE_PIN, GpioIrqFalling);
						
		// }
		
		if (timer_shield_observe > 0)
		{
			active_during_watch_flag = 1;	//在观察期触发

		}
		

        	Gpio_ClearIrq(QG_STATE_PORT, QG_STATE_PIN);
    	}

	if (Gpio_GetIrqStatus(MAIN_RI_PORT, MAIN_RI_PIN) == TRUE)
	{
		static uint8_t i = 0;
		Gpio_ClearIrq(MAIN_RI_PORT, MAIN_RI_PIN); //清除中断标志位
		wakeup_from_RI = 1; //设置唤醒标志
		if(0 == i)
          {
               Gpio_SetIO(APP_LED_SYS_PORT, APP_LED_SYS_PIN);  //LED翻转
               i++;
          }
          else
          {
               Gpio_ClrIO(APP_LED_SYS_PORT, APP_LED_SYS_PIN);  //LED翻转
               i = 0;
          }
	}
//     Gpio_SetIO(GpioPortA, GpioPin11);
//     Debug_Printf("\r\nenter int...\r\n");
//     Gpio_ClrIO(GpioPortA, GpioPin11);
//     Gpio_ClrIO(APP_LED_SYS_PORT, APP_LED_SYS_PIN);
}  

//模拟按键功能初始化，但不包括模拟红外触发
void QG_driver_init()
{
	stc_gpio_cfg_t stcGpioCfg;
    
	///< 打开GPIO外设时钟门控
	Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); 
	
	///< 端口方向配置->输出(其它参数与以上（输入）配置参数一致)
	stcGpioCfg.enDir = GpioDirIn;
	///< 端口上下拉配置->
	stcGpioCfg.enPu = GpioPuDisable;
	stcGpioCfg.enPd = GpioPdDisable;
	stcGpioCfg.enDrv = GpioDrvL;
     stcGpioCfg.enOD = GpioOdDisable;
	
	
	//读取电源开关状态引脚初始化
	Gpio_Init(QG_POWER_STATE_PORT, QG_POWER_STATE_PIN, &stcGpioCfg);
     stcGpioCfg.enPd = GpioPdDisable;
	///< 端口输入/输出值寄存器总线控制模式配置->AHB
//    	stcGpioCfg.enCtrlMode = GpioAHB;
	Gpio_Init(QG_STATE_PORT, QG_STATE_PIN, &stcGpioCfg);
	Gpio_EnableIrq(QG_STATE_PORT, QG_STATE_PIN, GpioIrqRising);		//启用外部中断
	// Gpio_EnableIrq(QG_STATE_PORT, QG_STATE_PIN, GpioIrqFalling); 
	EnableNvic(PORTB_IRQn, IrqLevel3, TRUE);

	//红外模拟触发初始化
	// stcGpioCfg.enDir = GpioDirOut;
	// stcGpioCfg.enPd = GpioPdEnable;
	// stcGpioCfg.enOD = GpioOdEnable;
	// Gpio_ClrIO(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
	// Gpio_Init(QG_DISPERSE_PORT, QG_DISPERSE_PIN, &stcGpioCfg);
	// Gpio_SetAnalogMode(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
	
}
//获取驱狗功能状态的高低电平
boolean_t QG_getStateStart()
{
	return Gpio_GetInputIO(QG_STATE_PORT, QG_STATE_PIN);
}

//红外模拟单独初始化
void QG_disperse_init()
{
	stc_gpio_cfg_t stcGpioCfg;
    
	///< 打开GPIO外设时钟门控
	Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); 
	
	///< 端口方向配置->输出(其它参数与以上（输入）配置参数一致)
	stcGpioCfg.enDir = GpioDirOut;
	///< 端口上下拉配置->
	stcGpioCfg.enPu = GpioPuDisable;
	stcGpioCfg.enPd = GpioPdDisable;
	stcGpioCfg.enDrv = GpioDrvL;
	stcGpioCfg.enOD = GpioOdDisable;
	
	Gpio_Init(QG_DISPERSE_PORT, QG_DISPERSE_PIN, &stcGpioCfg);
	Gpio_SetIO(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
}

//红外模拟反初始化为高阻态输入
void QG_disperse_deinit()
{
	stc_gpio_cfg_t stcGpioCfg;
	///< 打开GPIO外设时钟门控
	Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); 
	
	///< 端口方向配置->输出(其它参数与以上（输入）配置参数一致)
	// stcGpioCfg.enDir = GpioDirIn;
	stcGpioCfg.enDir = GpioDirOut;
	///< 端口上下拉配置->
	stcGpioCfg.enPu = GpioPuDisable;
	stcGpioCfg.enPd = GpioPdEnable;
	
	Gpio_Init(QG_DISPERSE_PORT, QG_DISPERSE_PIN, &stcGpioCfg);
	Gpio_ClrIO(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
}

boolean_t QG_getPowerState()
{
     return Gpio_GetInputIO(QG_POWER_STATE_PORT, QG_POWER_STATE_PIN);
}

boolean_t QG_getState()
{
     return Gpio_GetInputIO(QG_STATE_PORT, QG_STATE_PIN);
}

boolean_t QG_setDisperseState(boolean_t state)
{
	//初始化
	QG_disperse_init();
	if (state)
	{
		Gpio_ClrIO(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
		delay1ms(10);
		Gpio_SetIO(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
		
		
	}
	else
	{
		Gpio_SetIO(QG_DISPERSE_PORT, QG_DISPERSE_PIN);
	}
	//反初始化
	// QG_disperse_deinit();
	return TRUE;
}

void QG_set_low_power(void)
{
	if (CAT1_state_low_power == 1)
	{
		// Debug_Printf("\r\nCAT1 already in low power mode\r\n");
		Lpm_GotoDeepSleep(FALSE); 		//进入深度睡眠模式
	}else
	{
		// Debug_Printf("\r\nCAT1 enter low power mode\r\n");
		CAT1_MAIN_DTR_set(ON);		//拉高MAIN_DTR
		set_CAT1_enter_low_power();	//设置CAT1进入低功耗,先后顺序不能颠倒
		Lpm_GotoDeepSleep(FALSE); 	//进入深度睡眠模式
	}
}






// {
//   	"registry-mirrors" : [
// 		"https://docker.registry.cyou",
// 		"https://docker-cf.registry.cyou",
// 		"https://dockercf.jsdelivr.fyi",
// 		"https://docker.jsdelivr.fyi",
// 		"https://dockertest.jsdelivr.fyi",
// 		"https://mirror.aliyuncs.com",
// 		"https://dockerproxy.com",
// 		"https://mirror.baidubce.com",
// 		"https://docker.m.daocloud.io",
// 		"https://docker.nju.edu.cn",
// 		"https://docker.mirrors.sjtug.sjtu.edu.cn",
// 		"https://docker.mirrors.ustc.edu.cn",
// 		"https://mirror.iscas.ac.cn",
// 		"https://docker.rainbond.cc",
// 		"https://do.nark.eu.org",
// 		"https://dc.j8.work",
// 		"https://dockerproxy.com",
// 		"https://gst6rzl9.mirror.aliyuncs.com",
// 		"https://registry.docker-cn.com",
// 		"http://hub-mirror.c.163.com",
// 		"http://mirrors.ustc.edu.cn/",
// 		"https://mirrors.tuna.tsinghua.edu.cn/",
// 		"http://mirrors.sohu.com/" 
// 	],
//  	"insecure-registries" : [
//     		"registry.docker-cn.com",
//     		"docker.mirrors.ustc.edu.cn"
//     ],
// 	"debug": true,
// 	"experimental": false
// }


